//
//  CompEduViewController.m
//  fp
//
//  Created by xingshuai on 2020/11/14.
//

#import "CompEduViewController.h"

#import "ComEduTopView.h"
#import "RoomCollectionViewCell.h"
#import "XSCollectionViewFlowLayout.h"
#import "EduCellModel.h"

static NSString *identifier = @"XAComEduCollectionCellID";
static NSString *headerIdentifier = @"XAHeadFlowCollectionCellID";


@interface CompEduViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
// 数据展示
@property (nonatomic, strong) UICollectionView *collection;
// 头部分类按钮
@property (nonatomic, strong) ComEduTopView *headerV;
// 测试数据
@property (nonatomic, strong) NSMutableArray *topList;
// 测试数据
@property (nonatomic, strong) NSMutableArray *bottomList;

@end

@implementation CompEduViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 校师佳课模式
    self.currentnavType = XSCurrentNavViewTypeLesson;
    
    self.lessonLabel.text = @"素质教育";
    
    [self creatViewUI];
    [self requestData];
    [self requestBottomData];
}

// 初始化UI
-(void)creatViewUI {
    [self.view addSubview:self.collection];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.homeNavView.mas_bottom);
        //make.right.equalTo(self.view.mas_right).offset(-15);
        make.bottom.equalTo(self.view).offset(-5);
    }];
}

-(NSMutableArray *)topList {
    if (!_topList) {
        _topList = [[NSMutableArray alloc]init];
    }
    return _topList;
}

-(NSMutableArray *)bottomList {
    if (!_bottomList) {
        _bottomList = [[NSMutableArray alloc]init];
    }
    return _bottomList;
}

-(void)requestData {
    [self.topList removeAllObjects];
    NSArray *arr = @[@"德",@"智",@"体",@"美",@"劳",@"测试数据"];
    for (int i = 0; i < arr.count; i ++) {
        EduCellModel *model = [[EduCellModel alloc]init];
        model.labelStr = [arr objectAtIndex:i];
        if (i == 0) {
            model.isSelect = YES;
        }
        [self.topList addObject:model];
    }
}

-(void)requestBottomData {
    [self.bottomList removeAllObjects];
    NSArray *arr = @[@"全部",@"认识周围",@"医疗知识",@"认识自然",@"科学进校园",@"动物小常识",@"计算机基础",@"全部",@"认识周围",@"😠😠😠",@"认识自然",@"科学进校园",@"动物小常识",@"计算机基础"];
    for (int i = 0; i < arr.count; i ++) {
        EduCellModel *model = [[EduCellModel alloc]init];
        model.labelStr = [arr objectAtIndex:i];
        if (i == 1) {
            model.isSelect = YES;
        }
        [self.bottomList addObject:model];
    }
}

// 初始化collection
- (UICollectionView *)collection {
    if (!_collection) {
//        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//        flowLayout.minimumLineSpacing = 20;
//        flowLayout.minimumInteritemSpacing = 10;
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//        flowLayout.sectionHeadersPinToVisibleBounds = YES;
        
        XSCollectionViewFlowLayout *flowLayout = [[XSCollectionViewFlowLayout alloc] init];
        //设置悬停高度，这里如果写成0.就是没有悬停效果..
        flowLayout.naviHeight = -60;
        //设置滚动方向
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //左右间距
        flowLayout.minimumInteritemSpacing = 10;
        //上下间距
        flowLayout.minimumLineSpacing = 20;
        flowLayout.headerReferenceSize = CGSizeMake(ScreenWidth, 130);
        
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collection registerClass:[RoomCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        _collection.showsVerticalScrollIndicator = NO;
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.backgroundColor = [UIColor clearColor];
        _collection.showsVerticalScrollIndicator = NO;
        _collection.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
        
        //注册一种cell--->注册头部视图
        [_collection registerClass:[ComEduTopView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
    }
    return _collection;
}

#pragma mark - UICollectionViewDelegateFlowLayout
// 返回cell的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.collection.frame.size.width-60)/4, (self.collection.frame.size.width-60)/4);
}

#pragma mark - deleDate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 24;
}

//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView =nil;
    if (kind ==UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        self.headerV = (ComEduTopView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
        self.headerV.topList = self.topList;
        self.headerV.bottomList = self.bottomList;
        reusableView = self.headerV;
    }
    return reusableView;
}

// cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    RoomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 上滑偏移大于等于60添加背景色，防止headerview透明的方式覆盖在collection顶部
    if (scrollView.contentOffset.y >= 60) {
        self.headerV.backgroundColor = [UIColor whiteColor];
        self.collection.backgroundColor = [UIColor whiteColor];
        self.HomeNavColor = [UIColor whiteColor];
    } else {
        self.headerV.backgroundColor = [UIColor clearColor];
        self.collection.backgroundColor = [UIColor clearColor];
        self.HomeNavColor = [UIColor clearColor];
    }
    // 记录headerview的frame
    CGRect frame = self.headerV.frame;
    // 下拉固定头部视图位置
    if (scrollView.contentOffset.y <= 0) {
        frame.origin.y = scrollView.contentOffset.y;
    }
    // 重置headerview的frame
    [self.headerV setFrame:frame];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
